/*
 * @Author: zlx
 * @Date:   2018-02-27 10:08:24
 * @Last Modified by:   Marte
 * @Last Modified time: 2018-04-04 16:55:50
 */
/********************************测试服  *************************************/
// apicom 后台接口地址  rootdomain 页面路径   imgcom  图片路径
var apicom = "http://192.168.50.248:10001/";
//var imgcom="http://yst-images.img-cn-hangzhou.aliyuncs.com/";
/******************************** 正式服  *************************************/
//var apicom = "http://mobile.pujiyicheng.com";
var rootdomain = window.location.href.split('html')[0];
// var imgcom="http://yst-images.img-cn-hangzhou.aliyuncs.com/";
// 用户信息：
var user_info = {
	'id': '', //用户id
	'uuid': '', //uuid
	'thirdPartyType': '', //登录类型（1.QQ 2.微信）
	'phone': '', //手机号
	'logoAttachmentAddress': '', //头像
	'qqOppendId': '', //唯一标识
	'qqNickName': '', //昵称
	'wxOppendId': '', //微信唯一标识
	'wxNickName': '', //微信昵称
	'sex': '', //性别
	'mark': '', //来源: 0 安卓 1 iOS 2 PC
	'bindStatus': '',//绑定状态 1 QQ 2 微信 3 QQ +微信
	'token':''
};
var mark = NaN, //用户设备类型来源: 0 安卓 1 iOS 2 PC
	appid = '101465158', //web应用appid
	appkey = '34203678623796f66f1f210ec0ba4b8f'; //web应用APPkey
//用户所有设备型号
var userAgentInfo = navigator.userAgent;
//console.log(userAgentInfo);
// 红包金额
(function(win, doc) {
	// 设置根节点字体
	var resize = 'orientationchange' in window ? 'orientationchange' : 'resize';

	function change() {
		var html = doc.documentElement;
		var deviceWidth = html.clientWidth;
		html.style.fontSize = deviceWidth / 37.5 + 'px';
	}
	win.addEventListener(resize, change, false);
	win.addEventListener('load', change, false);
	doc.addEventListener('DOMContentLoaded', change, false);
	// 回退
	//	$(document).on('click','.back',function(){
	//		// 返回上一页
	//		window.history.go(-1);
	//		// 返回上一页  并刷新上一页
	//		// window.location.go(-1);
	//	})
	// 底部切换
	/*$(document).on('click', '.nav_index', function() {
		window.location.href = rootdomain + "html/homePage/sy.html";
	})
	$(document).on('click', '.nav_lietou', function() {
		window.location.href = rootdomain + "html/lietou/e_lietouhome.html";
	})
	$(document).on('click', '.nav_fabu', function() {
		//		window.location.href=rootdomain+"html/homePage/sy.html";
		particulars(0);
	})
	$(document).on('click', '.nav_fenlei', function() {
		window.location.href = rootdomain + "html/singlecenter/fenlei.html";
	})
	$(document).on('click', '.nav_wode', function() {
		window.location.href = rootdomain + "html/singlecenter/gerencenter_login.html";
	})*/
	//判断用户设备类型
	if (IsPC()) {
		mark = 2;
		return false;
	} else {
		if (/pujiyicheng/.test(userAgentInfo)) {
			//安卓手机
			mark = 0;
		} else if (/pjyc_i/.test(userAgentInfo)) {
			//苹果手机
			mark = 1;

		} else if (/MicroMessenger/i.test(userAgentInfo)) {
			//微信内置浏览器
			mark = 2;
		} else {
			//winphone 手机浏览器
			mark = 2;
		}
	}
})(window, document);
// 用户权限验证
function Auth() {
	var localStr = localStorage.getItem('user_info');
	if (localStr && localStr != "null" && localStr != "") {
		// 暂时关闭首页跳转 联调放开
		// window.location.href="home.html";
		user_info = JSON.parse(localStr);
		return true;
	} else {
		//window.location.href = "login.html";
		return false;
	}
}
// 弹出层 消息提示
function tips(msg) {
	layer.open({
		content: msg,
		skin: 'msg',
		time: 1, //2秒后自动关闭
		style: 'position:fixed;top:30%;left:50%;width:25rem;height:3rem;margin-left:-12.5rem;padding:10px 0; border:none;line-height:4rem'
	});
}

function big_tips(msg) {
	layer.open({
		content: msg,
		skin: 'msg',
		time: 1, //2秒后自动关闭
		style: 'position:fixed;top:30%;left:50%;width:25rem;height:6rem;margin-left:-12.5rem;padding:10px 0; border:none;line-height:4rem'
	});
}
//跳转原生 ios页面必须
function setupWebViewJavascriptBridge(callback) {
	if (window.WebViewJavascriptBridge) {
		callback(WebViewJavascriptBridge)
	} else {
		document.addEventListener(
			'WebViewJavascriptBridgeReady',
			function() {
				callback(WebViewJavascriptBridge)
			},
			false
		);
	}
	if (window.WVJBCallbacks) {
		return window.WVJBCallbacks.push(callback);
	}
	window.WVJBCallbacks = [callback];
	var WVJBIframe = document.createElement('iframe');
	WVJBIframe.style.display = 'none';
	WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
	document.documentElement.appendChild(WVJBIframe);
	setTimeout(function() {
		document.documentElement.removeChild(WVJBIframe)
	}, 0)
}
//跳转原生app
function particulars(type) {
	if (mark == 2) {
		tips('请下载app');
		return false;
	}
	//ios原生页面跳转
	if (/pujiyicheng/.test(ua)) {
		//安卓页面跳转
		//var result = lase;
		window.stub.jsToPublish(type); //用接口stub, 通过调用内部类中的方法jsMethod给java传回result。
	}
}
/* 公用全局函数 */
var globalFun = {
	/* 获取参数 */
	GetQueryString: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	},
	uniencode: function(text) {
		text = escape(text.toString()).replace(/\+/g, "%2B");
		var matches = text.match(/(%([0-9A-F]{2}))/gi);
		if (matches) {
			for (var matchid = 0; matchid < matches.length; matchid++) {
				var code = matches[matchid].substring(1, 3);
				if (parseInt(code, 16) >= 128) {
					text = text.replace(matches[matchid], '%u00' + code);
				}
			}
		}
		text = text.replace('%25', '%u0025');
		return text;
	}
}
//判断用户设备类型
//判断设备类型 先判断pc端
function IsPC() {
	var Agents = ["Android", "iPhone",
		"SymbianOS", "Windows Phone",
		"iPad", "iPod", "pujiyicheng", "pjyc_i", "MicroMessenger"
	];
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > -1) {

			flag = false;
			break;
		}
	}
	return flag;
}


//获取app用户登录信息
if (mark === 0) {
	function reloads() {
		var result = window.stub.clientToJsUserInfo();
		//tips(result);
		if (result != "null") {
			if (typeof(result) != "string") {
				user_info = result;
				var res = JSON.stringify(result);
				localStorage.setItem("user_info", res);
			} else {
				user_info = JSON.parse(result);
				localStorage.setItem("user_info", result);
			}

		} else {
			localStorage.removeItem("user_info")
		}
	}
	reloads()
} else if (mark === 1) {
	//在改function 中添加原生调起js方法
	setupWebViewJavascriptBridge(function(bridge) {
		bridge.callHandler('jsToUserInfo', function(resp) {
			if (resp) {
				localStorage.setItem("user_info", JSON.stringify(resp));
				user_info = resp;
			} else {
				localStorage.removeItem("user_info")
			}
		});
		//注册原生调起方法
		//参数1： buttonjs 注册flag 供原生使用，要和原生统一
		//参数2： data  是原生传给js 的数据
		//参数3： responseCallback 是js 的回调，可以通过该方法给原生传数据
		bridge.registerHandler("clientToJsUserInfo", function(data, responseCallback) {
			if (data != "null") {
				localStorage.setItem("user_info", JSON.stringify(data));
				user_info = data;
			}

		});

	})
}

/* 去跳转app登录页面 */
function toClientLogin(url) {
	if (mark === 0) {
		window.stub.jsToClientLogin(); //用接口stub, 通过调用内部类中的方法jsMethod给java传回result。
	}
	/* 跳转Ios App原生页面 */
	else if (mark === 1) {
		//在改function 中添加原生调起js方法
		setupWebViewJavascriptBridge(function(bridge) {

			bridge.callHandler('jsToClientLogin', function(resp) {});

		})
	} else {
		localStorage.setItem("currentUrl", url)
		sessionStorage.setItem('history_url', window.location.href);
		location.href = "../singlecenter/login_zm.html"
	}
}

/* 消息提示 */
var ShowMes = "null";

function ClientTojsShowMes(num) {
	ShowMes = num
	if (ShowMes != "null") {
		localStorage.setItem("ShowMes", ShowMes)
	}
}
if (mark === 1) {
	setupWebViewJavascriptBridge(function(bridge) {
		bridge.registerHandler('ClientTojsShowMes', function(data, resp) {
			ClientTojsShowMes(data)
		});
	})
}

//提示下载app
function downLoad() {
	tips('请下载普济一城App,解锁更多功能');
}
// android退出登录清楚本地用户信息的缓存
function jsToLoginAgain() {
	localStorage.removeItem("user_info");
}

if (mark == 1) {
	setupWebViewJavascriptBridge(function(bridge) {
		//退出登录清楚本地user_info
		bridge.registerHandler("jsToLoginAgain", function(data, responseCallback) {
			localStorage.removeItem("user_info");
		});
	});
}